﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace peonwar
{
        [Serializable]
    class UnitMedium3 : Unit
    {
        // ---CONSTRUCTORS---
        // Creation de l'unite en precisant l'instance du joueur "User"
        public UnitMedium3(User player) : base()
        {
            // Caracteristique au type d'unité
            UnitType = 6;
            QueueOffset = 50;

            Initialize(player);
        }

        // ---PROPERTIES---
        // ---METHODES---
        // Animation idle qui joue en boucle
        public override void AnimationIdle()
        {
            base.AnimationIdle();
            Texture = GameTexture.TextureUnitMedium3[0];
            Imgwidth = 35;
            Imgheight = 39;
            Indexmax = 5;

            Offset = 0;
        }

        // Animation walk qui est jouer en boucle une fois lancer
        public override void AnimationWalk()
        {
            base.AnimationWalk();
            Texture = GameTexture.TextureUnitMedium3[1];
            Imgwidth = 41;
            Imgheight = 37;
            Indexmax = 7;

            Offset = -5;
        }

        // Animation degain de l'arme qui lance ensuite l'animation tirer
        public override void AnimationDrawIn()
        {
            base.AnimationDrawIn();
            Texture = GameTexture.TextureUnitMedium3[2];
            Imgwidth = 80;
            Imgheight = 48;
            Indexmax = 17;

            Offset = 2;
        }

        // Animation tirer joue en boucle
        public override void AnimationFire()
        {
            base.AnimationFire();
            Texture = GameTexture.TextureUnitMedium3[3];
            Imgwidth = 70;
            Imgheight = 52;
            Indexmax = 27;

            Offset = 0;
        }

        // Animation rengainer l'arme qui lance ensuite l'animation marcher
        public override void AnimationDrawOut()
        {
            base.AnimationDrawOut();
            Texture = GameTexture.TextureUnitMedium3[4];
            Imgwidth = 80;
            Imgheight = 48;
            Indexmax = 14;

            Offset = 0;
        }

        // Animation mourrir, pas besoin de l'expliquer
        public override void AnimationDie()
        {
            base.AnimationDie();
            Texture = GameTexture.TextureUnitMedium3[5];
            Imgwidth = 60;
            Imgheight = 40;
            Indexmax = 9;

            Offset = 0;
        }

        protected override void Fire(bool firebase, User enemy)
        {
            int sign = -1;
            if (TeamBlue)
                sign = 1;

            Vector2 P = FindEnemy(enemy.PeonList, 0);
            if (P == Vector2.Zero)
                P = new Vector2((enemy.SpawnPosition.X) - (sign * 200), this.Position.Y);

            User.EffectList.Add(new AnimatedEffect(P.X, P.Y - 20, TeamBlue, GameTexture.TextureEffects[1], 62, 63, 3));
            Random rand = new Random();

            for (int i = (int)this.Position.X + (14 * sign); i * sign < (int)P.X * sign; i += 6 * sign)
            {
                double vect = rand.NextDouble() * 2 * Math.PI;
                Vector2 dir = new Vector2((float)Math.Cos(vect), (float)Math.Sin(vect));
                int r = rand.Next(2);
                User.AdvertList.Add(new Advert(new Vector2(i, P.Y - 15 - rand.Next(10)), r == 1 ? GameData.Font_Cursor_Little : GameData.Font_Cursor_Medium, r == 1 ? "o" : ".", 0.5f + rand.Next(2), dir, 0.15f, r == 1 ? Color.Gray : Color.DarkGray, true, true));
            }
        }

        private Vector2 FindEnemy(List<Unit> list, int index)
        {
            if (index < list.Count())
            {
                if (list[index].DeadTime == 0)
                    return list[index].Position;
                else
                    return FindEnemy(list, index + 1);
            }
            else
                return new Vector2(0, 0);
        }
    }
}

